interface FoodInfo {
  foodImg: ResourceStr,
  foodName: string,
  foodPrice: number
}


@Entry
@Component
struct HomeworkPage1 {
  @State foodImgList: FoodInfo[] = [
    { foodImg: $r("app.media.goods1"), foodName: "盛夏海鲜家宴", foodPrice: 4999 },
    { foodImg: $r("app.media.goods2"), foodName: "精品西式家宴", foodPrice: 1299 },
    { foodImg: $r("app.media.goods3"), foodName: "精品皇家家宴", foodPrice: 2599 },
    { foodImg: $r("app.media.goods4"), foodName: "暖冬团聚家宴", foodPrice: 1899 }
  ]
  scroller: Scroller = new Scroller()

  build() {
    Column() {
      Row(){
        Divider()
          .width(60)
          .margin({right:10})
        Text()
          .backgroundColor(Color.Gray)
          .width(6)
          .height(6)
          .borderRadius(50)
          .margin({right:10})
        Text("优选餐宴")
          .id('HomeworkPage2HelloWorld')
          .fontSize(30)
          .fontWeight(FontWeight.Bold)
        Text()
          .backgroundColor(Color.Gray)
          .width(6)
          .height(6)
          .borderRadius(50)
          .margin({left:10})
        Divider()
          .width(60)
          .margin({left:10})
      }


      Text("Scroll组件")
        .fontSize(20)
        .fontColor('red')
      Scroll(this.scroller) {
        Row({ space: 20 }) {
          ForEach(this.foodImgList, (item: FoodInfo) => {
            Column() {
              Image(item.foodImg)
                .width("100%")
                .height(100)
              Text(item.foodName)
                .fontSize(20)
                .fontWeight(FontWeight.Bold)
              Text(`￥${item.foodPrice}/桌`)
                .fontSize(16)
                .fontColor("#e06d64")
            }.width(150)
            .height("100%")
            .alignItems(HorizontalAlign.Start)
          })
        }.height("100%")
      }.height(150)
      .width("100%")
      .scrollable(ScrollDirection.Horizontal)

      Text("Grid组件")
        .margin({ top: 20 })
        .fontSize(20)
        .fontColor('red')
      Grid() {
        ForEach(this.foodImgList, (item: FoodInfo) => {
          GridItem() {
            Column() {
              Image(item.foodImg)
                .width("100%")
                .height(100)
              Text(item.foodName)
                .fontSize(20)
                .fontWeight(FontWeight.Bold)
              Text(`￥${item.foodPrice}/桌`)
                .fontSize(16)
                .fontColor("#e06d64")
            }.width(150)
            .height("100%")
            .alignItems(HorizontalAlign.Start)
          }
        })
      }
      .height(150)
      .width("100%")
      .rowsTemplate('1fr')
      .columnsGap(20)
      .enableScrollInteraction(true)

      Text("List组件")
        .margin({ top: 20 })
        .fontSize(20)
        .fontColor('red')
      List({ space: 20, scroller: this.scroller }) {
        ForEach(this.foodImgList, (item: FoodInfo) => {
          ListItem() {
            Column() {
              Image(item.foodImg)
                .width("100%")
                .height(100)
              Text(item.foodName)
                .fontSize(20)
                .fontWeight(FontWeight.Bold)
              Text(`￥${item.foodPrice}/桌`)
                .fontSize(16)
                .fontColor("#e06d64")
            }.width(150)
            .height("100%")
            .alignItems(HorizontalAlign.Start)
          }
        })
      }.height(150)
      .listDirection(Axis.Horizontal)
    }
    .height('100%')
    .width('100%')
  }
}



————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

interface Contact {
  contactName: string
  contactNumber: number
}

interface GroupItem {
  groupName: string
  children: Contact[]
}

interface FlatContact {
  groupName: string
  contactName: string
  contactNumber: number
}

// 二维数组转一维数组
function flatten2DArray(groups: GroupItem[]): FlatContact[] {
  let result: FlatContact[] = []
  groups.forEach((group) => {
    group.children.forEach((contact) => {
      result.push({
        groupName: group.groupName,
        contactName: contact.contactName,
        contactNumber: contact.contactNumber
      })
    })
  })
  return result
}

// 一维数组转二维数组
function group1DArray(contacts: FlatContact[]): GroupItem[] {
  let groupMap: Map<string, Contact[]> = new Map()

  contacts.forEach((contact) => {
    if (!groupMap.has(contact.groupName)) {
      groupMap.set(contact.groupName, [])
    }
    groupMap.get(contact.groupName)?.push({
      contactName: contact.contactName,
      contactNumber: contact.contactNumber
    })
  })

  let result: GroupItem[] = []
  groupMap.forEach((children, groupName) => {
    result.push({
      groupName: groupName,
      children: children
    })
  })

  return result
}

// 测试用例
let twoDArray: GroupItem[] =
  [
    {
      groupName: 'A',
      children: [
        {
          contactName: 'Andy',
          contactNumber: 188
        },
        {
          contactName: 'An',
          contactNumber: 189
        },
        {
          contactName: 'Anna',
          contactNumber: 190
        }
      ]
    },
    {
      groupName: 'B',
      children: [
        {
          contactName: 'Bush',
          contactNumber: 120
        },
        {
          contactName: 'Bob',
          contactNumber: 121
        }
      ]
    },
    {
      groupName: 'C',
      children: [
        {
          contactName: 'Candy',
          contactNumber: 200
        },
        {
          contactName: 'Con',
          contactNumber: 201
        }
      ]
    }
  ]

let oneDArray: FlatContact[] =
  [
    {
      groupName:'A',
      contactName: 'Andy',
      contactNumber: 188
    },
    {
      groupName:'A',
      contactName: 'An',
      contactNumber: 189
    },
    {
      groupName:'A',
      contactName: 'Anna',
      contactNumber: 190
    },
    {
      groupName:'B',
      contactName: 'Bush',
      contactNumber: 120,
    },
    {
      groupName:'B',
      contactName: 'Bob',
      contactNumber: 121
    },
    {
      groupName:'C',
      contactName: 'Candy',
      contactNumber: 200
    }
  ]

console.log('二维转一维:', JSON.stringify(flatten2DArray(twoDArray)))
console.log('一维转二维:', JSON.stringify(group1DArray(oneDArray)))